package hashTable;

public class Hash {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);
        Emp emp1 = new Emp(1, "zhangsan");
        Emp emp8 = new Emp(8, "zhangsan");
        Emp emp2 = new Emp(2, "zhangsan");
        Emp emp3 = new Emp(3, "zhangsan");
        hashTable.add(emp1);
        hashTable.add(emp8);
        hashTable.add(emp2);
        hashTable.add(emp3);
        hashTable.list();
        hashTable.findEmpById(4);
    }
}

//创建hashTable
class HashTable {
    private EmpLinkedList[] empLinkedListsArray;
    int size;

    public HashTable(int size) {
        empLinkedListsArray = new EmpLinkedList[size];
        this.size = size;
        //分别初始化每个链表
        for (int i = 0; i < size; i++) {
            empLinkedListsArray[i] = new EmpLinkedList();
        }
    }

    public void add(Emp emp) {
        //根据员工的id，得到该员工应当添加到那条链表
        int empLinkedListNO = hashCode(emp.id);
        empLinkedListsArray[empLinkedListNO].add(emp);
    }

    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListsArray[i].list();
        }
    }

    public int hashCode(int id) {
        return id % size;
    }
    public void findEmpById(int id){
        int empLinkedListNO =hashCode(id);
        Emp emp=empLinkedListsArray[empLinkedListNO].findEmpById(id);

        if (emp !=null){
            System.out.println("查找到了"+emp.toString());
        }else{
            System.out.println("没有找到");
        }
    }
}


class EmpLinkedList {
    private Emp head;

    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        curEmp.next = emp;
    }

    public void list() {
        if (head == null) {
            System.out.println("当前链表为空");
            return;
        }
        System.out.println("当前链表信息为:");
        Emp curEmp = head;
        while (true) {
            System.out.printf("=> id=%d,name=%s", curEmp.id, curEmp.name);
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }

    public Emp findEmpById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return null;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.id == id) {
                break;
            }
            if (curEmp.next == null) {
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }
}

class Emp {
    public int id;
    public String name;
    public Emp next;

    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", next=" + next +
                '}';
    }
}